package com.zhkj.txg.module.order.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.order.popup.ShippingCompanyPopupWindow
import com.zhkj.txg.module.order.vm.RefundViewModel
import com.zhkj.txg.utils.ImageUtils
import kotlinx.android.synthetic.main.activity_refund_shipping.*
import kotlinx.android.synthetic.main.item_order_goods.*

class OrderRefundShippingActivity : ViewModelActivity<RefundViewModel>() {

    companion object {
        private val ORDER_ID = "orderId"
        private val GOODS_ID = "goodsId"
        private val ORDER_GROUP = "ORDER_GROUP"//拼团订单

        fun open(activity: Activity, orderId: Long, goodsId: Long, requestCode: Int, isGroup: Boolean = false) {
            val intent = Intent(activity, OrderRefundShippingActivity::class.java)
                .putExtra(ORDER_ID, orderId).putExtra(GOODS_ID, goodsId)

            if (isGroup) {
                intent.putExtra(ORDER_GROUP, isGroup)
            }
            activity.startActivityForResult(intent, requestCode)
        }
    }

    var isGroup: Boolean = false
    override fun setViewModel(): RefundViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleOrderRefundShipping
    override val layoutResId: Int = R.layout.activity_refund_shipping

    private val companyPopupWindow: ShippingCompanyPopupWindow by lazy {
        ShippingCompanyPopupWindow(this) { companyEntity ->
            tvRefundShippingCompany.text = companyEntity.name
            viewModel.deliveryEntity = companyEntity
            companyPopupWindow.dismiss()
        }
    }

    override fun initUI() {
        viewRefundShipping.setOnClickListener {
            companyPopupWindow.showPopupWindow()
        }
        tvRefundShippingConfirm.setOnClickListener {
            if (viewModel.deliveryEntity == null) {
                toast(getString(R.string.refund_shipping_company_hint))
                return@setOnClickListener
            }
            if (etRefundShippingNo.text.isNullOrEmpty()) {
                toast(getString(R.string.refund_shipping_no_hint))
                return@setOnClickListener
            }
            viewModel.submitDelivery(AccountManager.getToken(), etRefundShippingNo.text.toString(), isGroup)
        }
    }

    override fun initData() {
        viewModel.entity.orderId = intent.getLongExtra(ORDER_ID, 0)
        viewModel.entity.goodsId = intent.getLongExtra(GOODS_ID, 0)
        viewModel.refundGoods(AccountManager.getToken(), isGroup)
        viewModel.deliveryList()
    }

    override fun initViewModel() {
        isGroup = intent.getBooleanExtra(ORDER_GROUP, false)
        viewModel.deliveryListLiveData.observe(this, Observer { response ->
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.let {
                companyPopupWindow.setData(it.data)
            }
        })
        viewModel.submitDeliveryListLiveData.observe(this, Observer {
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS) {
                toast(it.response?.msg ?: getString(R.string.submit_success))
                setResult(RESULT_OK)
                finish()
            }
        })
        viewModel.refundGoodsLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let {
                ImageUtils.loadRoundCornerImage(this, ivOrderGoods, it.image)
                tvOrderGoodsName.text = it.goodsName
                tvOrderGoodsPrice.text = SpanUtil.setMoneySymbol(it.goodsPrice)
                tvOrderGoodsNumber.text = "x${it.goodsNumber}"
            }
        })
    }
}